<template>
  <div>
    <el-dialog
      v-model="_dialogVisible"
      :title="`${role_id ? '修改' : '添加'}角色`"
      :before-close="handleClose"
      destroy-on-close
      draggable
    >
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="100">
        <el-form-item label="角色名称" prop="role_name">
          <el-input v-model="formData.role_name" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="权限标识符" prop="role_key">
          <el-input
            v-model="formData.role_key"
            placeholder="请输入权限标识符"
          />
        </el-form-item>
        <el-form-item label="角色状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio-button :label="1">启用</el-radio-button>
            <el-radio-button :label="2">禁用</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" v-model="formData.description"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="handleSubmit">
          {{ role_id ? '修改' : '添加' }}
        </el-button>
        <el-button @click="handleClose">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, watch } from 'vue'
import { RoleParams } from '@/api/auth/model/roleModel'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { useForm } from '@/hooks'
import { show, store, update } from '@/api/auth/role'

const props = defineProps({
  dialogVisible: {
    type: Boolean,
    default: false,
  },
  role_id: {
    type: Number,
    default: 0,
  },
})

const { validate, resetForm } = useForm()
const emit = defineEmits(['update:dialogVisible', 'success'])
const formRef = ref<FormInstance>(null!)

const _dialogVisible = ref<boolean>(false)

let formData = ref<RoleParams | any>({
  role_id: 0,
  role_name: '',
  role_key: '',
  description: '',
  status: 1,
})

watch(
  () => props.dialogVisible,
  () => {
    _dialogVisible.value = props.dialogVisible
  }
)

watch(
  () => _dialogVisible.value,
  () => {
    emit('update:dialogVisible', _dialogVisible.value)
  }
)

//获取数据
const detail = async () => {
  if (props.role_id) {
    const { code, message, data } = await show({ role_id: props.role_id })
    if (code != 200) {
      ElMessage.error(message)
      return
    }
    formData.value = data
  }
}

watch(
  () => [props.role_id, props.dialogVisible],
  () => {
    props.dialogVisible && props.role_id && detail()
  }
)

const handleClose = () => {
  resetForm(formRef.value)
  _dialogVisible.value = false
}

const rules = reactive<FormRules<RoleParams>>({
  role_name: [
    {
      required: true,
      message: '请输入角色名称',
      trigger: 'blur',
    },
  ],
  role_key: [
    {
      required: true,
      message: '请输入权限标识符',
      trigger: 'blur',
    },
  ],
  status: [
    {
      required: true,
      message: '请选择角色状态',
      trigger: 'blur',
    },
  ],
})

const handleSubmit = async () => {
  await validate(formRef.value)
  //发送请求
  let result
  if (!props.role_id) {
    result = await store(formData.value)
  } else {
    result = await update(formData.value)
  }
  const { code, message } = result
  if (code != 200) {
    ElMessage.error(`${props.role_id ? '修改' : '添加'}${message}`)
    return
  }
  handleClose()
  ElMessage.success(`${props.role_id ? '修改' : '添加'}${message}`)
  emit('success')
}
</script>

<style scoped></style>
